/**
 * RequestModel.java
 *
 * Created on 26 Март 2008 г., 21:08
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package trim.gui.testing.models;

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Дмитрий
 */
public class RequestModel extends AbstractTableModel
{
  private List<Integer> formats=new ArrayList();
  private List<Integer> fCount=new ArrayList();
  public int getRowCount()
  {
    return getFormats().size()+2;
  }
  
  public int getColumnCount()
  {
    return 2;
  }
  
  @Override
  public String getColumnName(int col)
  {
    switch(col)
    {
      case 0:return "Format";
      case 1:return "Count";
    }
    return "";
  }
  
  @Override
  public boolean isCellEditable(int row,int col)
  {
    return row<formats.size();
  }
  
  
  public Object getValueAt(int rowIndex, int columnIndex)
  {
    if(rowIndex<formats.size())
    {
      switch(columnIndex)
      {
        case 0:return getFormats().get(rowIndex);
        case 1:return getFCount().get(rowIndex);
      }
    }
    else
    {
      if(rowIndex==formats.size())
      {
        switch(columnIndex)
        {
          case 0:return "Count";
          case 1:
            return formats.size();
        }
      }
      else
      {
        switch(columnIndex)
        {
          case 0:return "Total";
          case 1:
            Integer x=0;
            for(Integer d:fCount)
              x+=d;
            return x;
        }
      }
    }
    return null;
  }
  
  
  public void addLine()
  {
    getFormats().add(0);
    getFCount().add(0);
    fireTableRowsInserted(getRowCount(),getRowCount());
  }
  
  @Override
  public void setValueAt(Object aValue, int rowIndex, int columnIndex)
  {
    switch(columnIndex)
    {
      case 0:getFormats().set(rowIndex,Integer.parseInt(aValue.toString()));break;
      case 1:getFCount().set(rowIndex,Integer.parseInt(aValue.toString()));break;
    }
    //fireTableDataChanged();
    fireTableRowsUpdated(rowIndex,rowIndex);
  }
  
  
  public List<Integer> getFormats()
  {
    return formats;
  }
  
  
  public List<Integer> getFCount()
  {
    return fCount;
  }
  
  public void setFormats(List<Integer> formats)
  {
    this.formats = formats;
  }
  
  public void setFCount(List<Integer> fCount)
  {
    this.fCount = fCount;
  }
  
}
